﻿#include "gamerule.h"

gamerule::gamerule()
{

}

gamerule::gamerule(QWidget *event):QWidget(event){
    setFixedSize(1100, 980);
}

int score1;
int score2;

void gamerule::init_tank(tank &t1, tank &t2){
    t1.x=150;
    t1.y=200;
    t1.angle=0;

    t2.x=950;
    t2.y=850;
    t2.angle=180;
}

void gamerule::isWin(tank &t1,tank &t2){
    if(t1.isLive==1&&t2.isLive==0){
        if(clock()-t2.Record_Live_Time>2000){
            t1.grade++;
            t2.isLive=1;
            init_tank(t1,t2);
            get_socore(t1,t2);
            t1.count_xy(t1);
            t2.count_xy(t2);
            is_up=1;
        }
    }
    else if(t1.isLive==0&&t2.isLive==1){
        if(clock()-t1.Record_Live_Time>2000){
            t2.grade++;
            t1.isLive=1;
            init_tank(t1,t2);
            get_socore(t1,t2);
            t1.count_xy(t1);
            t2.count_xy(t2);
            is_up=1;
        }
    }
    else if(t1.isLive==0&&t2.isLive==0){
        if(clock()-t1.Record_Live_Time>2000||clock()-t2.Record_Live_Time>2000){
            t1.isLive=1;
            t2.isLive=1;
            init_tank(t1,t2);
            get_socore(t1,t2);
            t1.count_xy(t1);
            t2.count_xy(t2);
            is_up=1;
        }
    }
}

void gamerule::paintEvent(QPaintEvent *event){
    Q_UNUSED(event);
    QPainter painter(this);
    QFont font("Arial",20);
    painter.setFont(font);
    QString text = QString("Player1: %1    Player2: %2")
                          .arg(score1).arg(score2);
    painter.drawText(300,100,text);
}

void gamerule::get_socore(tank &t1, tank &t2){
    score1=t1.grade;
    score2=t2.grade;
}
